<?php

namespace XWAM\Controller;

/**
 * 登录日志 控制器类
 *
 * @since 1.0 <2017-3-27> SoChishun <14507247@qq.com> Added.
 */
class LoginLog extends AppbaseController {

    function indexSearch() {
        $search = I('param.');
        $where = array();
        if (!empty($search['word'])) {
            $where['title'] = array('like', '%' . $search['word'] . '%');
        }
        if (!empty($search['title'])) {
            $where['title'] = array('like', '%' . $search['title'] . '%');
        }
        // $where['site_id'] = $this->site_id;
        return array('search' => $search, 'where' => $where);
    }

    function index() {
        $asearch = $this->indexSearch();
        $mLog = new \XWAM\Model\LoginLogModel();
        $list = $mLog->selectPaging($page, array('where' => $asearch['where'], 'order' => 'sort, id desc'), array('page_params' => $asearch['search']));
        $this->assign('search', $asearch['search']);
        $this->assign('list', $list);
        $this->assign('page', $page->showBootstrap());
        $this->display();
    }

    function edit($id = 0, $category_id = 0) {
        $data = array();
        if ($id) {
            $mLog = new \XWAM\Model\LoginLogModel();
            $data = $mLog->find($id);
        }
        if (!$data) {
            $data = array('category_id' => $category_id, 'user_name' => $this->userName, 'site_id' => $this->siteId);
        }
        $this->assign('data', $data);
        $this->display();
    }

    function editSave() {
        $mLog = new \XWAM\Model\LoginLogModel();
        $result = $mLog->saveArticle();
        $this->dialogJump($result['status'], $result['info']);
    }

    function changeStatus($id, $status) {
        $mLog = new \XWAM\Model\LoginLogModel();
        $this->ajaxReturn($mLog->changeStatus($id, $status));
    }

    function changeSort($id, $sort) {
        $mLog = new \XWAM\Model\LoginLogModel();
        $this->ajaxReturn($mLog->changeSort($id, $sort));
    }

    function delete($id = '') {
        $mLog = new \XWAM\Model\LoginLogModel();
        $result = $mLog->deleteArticle($id);
        $this->ajaxMsg($result['status'], $result['info']);
    }

    // 2017-3-20
    function category($id = '') {
        if (!$id) {
            $this->dialogClose(false, array('error' => '参数有误!'));
        }
        $mLog = new \XWAM\Model\LoginLogModel();
        $list = $mLog->where(array('id' => array('in', $id)))->field('id, title, category_id')->order('sort, id desc')->select();
        $this->assign('list', $list);
        $this->display();
    }

    /**
     * 保存类别
     * @since 1.0 <2016-3-20> SoChishun Added.
     */
    public function categorySave() {
        $aid = I('id');
        $aCategoryId = I('category_id');
        $mLog = new \XWAM\Model\LoginLogModel();
        $result = $mLog->saveCategory($aid, $aCategoryId);
        if (!$result['status']) {
            $this->dialogClose(false, array('error' => '参数有误!'));
        }
        $this->dialogJump(true);
    }

}
